using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using Entity;
using Utilities;


namespace DAL
{
    public class InPanelDataAccess : FreshCmsAccessBase
    {
        #region code CMS
        public List<InPanel> GetInPanelList()
        {
            try
            {
                const string spName = "spi_InPanel_GetList";

                var InPanelList = DbHelper.ExecuteSPList<InPanel>(spName);
                return InPanelList;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return new List<InPanel>();
            }
        }

        public List<InPanel> GetInPanelListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        {
            try
            {
                const string spName = "spi_InPanel_GetByPaging";
                var sqlParams = new[]
                                    {
										new SqlParameter("@PageIndex", pageIndex),
										new SqlParameter("@PageSize", pageSize),
										new SqlParameter("@TotalRecord", totalRecord)
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
                var InPanelList = DbHelper.ExecuteSqlList<InPanel>(sqlCommand);
				if (sqlCommand.Parameters["@TotalRecord"].Value != null)
                {
                    totalRecord = (int) sqlCommand.Parameters["@TotalRecord"].Value;
                }

                return InPanelList;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return new List<InPanel>();
            }
        }

        public InPanel GetInPanelById(int panelId)
        {
            try
            {
                const string spName = "spi_InPanel_GetById";
                var sqlParams = new[]
                                    {
										new SqlParameter("@PanelId", panelId)
                                    };

                var InPanel = DbHelper.ExecuteSPList<InPanel>(spName, sqlParams).SingleOrDefault();
                return InPanel;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return null;
            }
        }

        public int InsertInPanel(InPanel InPanel)
        {
            try
            {
                const string spName = "spi_InPanel_Insert";
                var sqlParams = new[]
                                    {

										new SqlParameter("@PanelParentId", InPanel.PanelParentId),
										new SqlParameter("@PanelName", InPanel.PanelName),
										new SqlParameter("@PanelSatus", InPanel.PanelSatus),
										new SqlParameter("@PanelId", InPanel.PanelId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@PanelId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@PanelId"].Value != null)
                {
                    return (int) sqlCommand.Parameters["@PanelId"].Value;
                }

				return 0;


            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        public int UpdateInPanel(InPanel InPanel)
        {
            try
            {
                const string spName = "spi_InPanel_Update";
                var sqlParams = new[]
                                    {
										new SqlParameter("@PanelId", InPanel.PanelId)
										, new SqlParameter("@PanelParentId", InPanel.PanelParentId)
										, new SqlParameter("@PanelName", InPanel.PanelName)
										, new SqlParameter("@PanelSatus", InPanel.PanelSatus)
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        public int DeleteInPanel(int panelId)
        {
            try
            {
                const string spName = "spi_InPanel_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@PanelId", panelId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }
		#endregion
    }
}
